home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / FACTORYDISPATCHER.PY < prev    next >
Encoding:
Python Source  |  2000-09-07  |  6.9 KB  |  187 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64.  
  65.  
  66. # Implement the manage_addProduct method of object managers
  67. import Acquisition, sys, Products
  68. from string import rfind
  69. from AccessControl.PermissionMapping import aqwrap
  70. from AccessControl.Owned import UnownableOwner
  71.  
  72. class ProductDispatcher(Acquisition.Implicit):
  73.     " "
  74.     # Allow access to factory dispatchers
  75.     __allow_access_to_unprotected_subobjects__=1
  76.  
  77.     def __getitem__(self, name):
  78.         return self.__bobo_traverse__(None, name)
  79.  
  80.     def __bobo_traverse__(self, REQUEST, name):
  81.         product=self.aq_acquire('_getProducts')()._product(name)
  82.  
  83.         # Try to get a custom dispatcher from a Python product
  84.         dispatcher_class=getattr(
  85.             getattr(Products, name, None),
  86.             '__FactoryDispatcher__',
  87.             FactoryDispatcher)
  88.  
  89.         dispatcher=dispatcher_class(product, self.aq_parent, REQUEST)
  90.         return dispatcher.__of__(self)
  91.  
  92. class FactoryDispatcher(Acquisition.Implicit):
  93.     """Provide a namespace for product "methods"
  94.     """
  95.  
  96.     _owner=UnownableOwner
  97.  
  98.     def __init__(self, product, dest, REQUEST=None):
  99.         if hasattr(product,'aq_base'): product=product.aq_base
  100.         self._product=product
  101.         self._d=dest
  102.         if REQUEST is not None:
  103.             try:
  104.                 v=REQUEST['URL']
  105.             except KeyError: pass
  106.             else:
  107.                 v=v[:rfind(v,'/')]
  108.                 self._u=v[:rfind(v,'/')]
  109.  
  110.     def Destination(self):
  111.         "Return the destination for factory output"
  112.         return self.__dict__['_d'] # we don't want to wrap the result!
  113.     this=Destination
  114.     this__roles__=Destination__roles__=None
  115.     
  116.  
  117.     def DestinationURL(self):
  118.         "Return the URL for the destination for factory output"
  119.         url=getattr(self, '_u', None)
  120.         if url is None:
  121.             url=self.Destination().absolute_url()
  122.         return url
  123.  
  124.     DestinationURL__roles__=None
  125.  
  126.     def __getattr__(self, name):
  127.         p=self.__dict__['_product']
  128.         d=p.__dict__
  129.         if hasattr(p,name) and d.has_key(name):
  130.             m=d[name]
  131.             w=getattr(m, '_permissionMapper', None)
  132.             if w is not None:
  133.                 m=ofWrapper(aqwrap(m, getattr(w,'aq_base',w), self))
  134.     
  135.             return m
  136.  
  137.         # Waaa
  138.         m='Products.%s' % p.id
  139.         if sys.modules.has_key(m) and sys.modules[m]._m.has_key(name):
  140.             return sys.modules[m]._m[name]
  141.     
  142.         raise AttributeError, name
  143.  
  144.     # Provide acquired indicators for critical OM methods:
  145.     _setObject=_getOb=Acquisition.Acquired
  146.  
  147.     # Make sure factory methods are unowned:
  148.     _owner=UnownableOwner
  149.  
  150.     # Provide a replacement for manage_main that does a redirection:
  151.     def manage_main(trueself, self, REQUEST, update_menu=0):
  152.         """Implement a contents view by redirecting to the true view
  153.         """
  154.         d = update_menu and '/manage_main?update_menu=1' or '/manage_main' 
  155.         REQUEST['RESPONSE'].redirect(self.DestinationURL()+d)
  156.  
  157.  
  158. import ExtensionClass
  159. class ofWrapper(ExtensionClass.Base):
  160.     def __init__(self, o):
  161.         self._o=o
  162.  
  163.     def __of__(self, parent): return self.__dict__['_o']
  164.  
  165.  
  166.